package com.meichis.yndms.adapter.RecycleViewItemType;

import android.app.DatePickerDialog;
import android.graphics.Color;
import android.text.Html;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;

import com.meichis.mcsappframework.adapter.recyclerview.base.ItemViewDelegate;
import com.meichis.mcsappframework.adapter.recyclerview.base.ViewHolder;
import com.meichis.mcsappframework.utils.DateUtil;
import com.meichis.mcsappframework.utils.MessageTools;
import com.meichis.yndms.R;
import com.meichis.yndms.component.ChoicePopView;
import com.meichis.yndms.model.DicDataItem;
import com.meichis.yndms.model.InspectTemplate_Item;

import java.util.ArrayList;
import java.util.Calendar;

public class FillMode3_TextView implements ItemViewDelegate<String> {

    private ArrayList<InspectTemplate_Item> items;
    private ArrayList<DicDataItem> dicDataItems;
    private Calendar mCalendar;

    public FillMode3_TextView(ArrayList<InspectTemplate_Item> items, ArrayList<DicDataItem> dicDataItems) {
        this.items = items;
        this.dicDataItems = dicDataItems;
        mCalendar = Calendar.getInstance();
    }

    @Override
    public int getItemViewLayoutId() {
        return R.layout.fill_mode_3_text;
    }

    @Override
    public boolean isForViewType(String s, int position) {
        //空白 横向标题 竖向品项 mode 6，7
        return position == 0 || position <= items.size() || position % (items.size() + 1) == 0 || items.get(getTempPosition(position)).getItem().getFillMode() == 6 || items.get(getTempPosition(position)).getItem().getFillMode() == 7;
    }

    @Override
    public void convert(final ViewHolder holder, final String s, int position) {
        final TextView tv_answer = holder.getView(R.id.tv_answer);
        tv_answer.setTextColor(Color.BLACK);
        if (position == 0) {
            tv_answer.setText("项目");
            return;
        }
        //横向标题
        if (position <= items.size()) {
            tv_answer.setTextColor(Color.BLACK);
            tv_answer.setText(Html.fromHtml(items.get(position - 1).getItem().getName() +
                    (!TextUtils.isEmpty(items.get(position - 1).getItem().getUnit()) ? "(" + items.get(position - 1).getItem().getUnit() + ")" : "") +
                    (items.get(position - 1).getIsMustFill() == 1 ? " <font  color=\"#FF0000\">*</font>" : "") +
                    (!TextUtils.isEmpty(items.get(position - 1).getItem().getSubTitle()) ? "<br/> <font color=\"#8D8D8D\" ><small>" + items.get(position - 1).getItem().getSubTitle() + "</small></font>" : "")
            ));
            return;
        }
        //竖向品项
        if (position % (items.size() + 1) == 0) {
            tv_answer.setTextColor(Color.BLACK);
            tv_answer.setText(dicDataItems.get(position / (items.size() + 1) - 1).getName());
            return;
        }
        //填写内容
        try {
            String answer = dicDataItems.get(getDicPosition(position)).getResult().getItems().get(getTempPosition(position)).getFillValue();
            if (answer == null) {
                answer = items.get(getTempPosition(position)).getFillName();
            }
            dicDataItems.get(getDicPosition(position)).getResult().getItems().get(getTempPosition(position)).setFillName(answer);
            dicDataItems.get(getDicPosition(position)).getResult().getItems().get(getTempPosition(position)).setFillValue(answer);
            tv_answer.setText(answer);
        } catch (Exception e) {

        }
        tv_answer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final InspectTemplate_Item item = items.get(getTempPosition(holder.getAdapterPosition()));
                switch (items.get(getTempPosition(holder.getAdapterPosition())).getItem().getFillMode()) {
                    case 6:
                        new ChoicePopView<>(holder.getConvertView().getContext(), item.getItem().getOptions(), "Name", new ChoicePopView.SelectedListener() {
                            @Override
                            public void choice(int position) {
                                tv_answer.setText(item.getItem().getOptions().get(position).getName());
                                dicDataItems.get(getDicPosition(holder.getAdapterPosition())).getResult().getItems().get(getTempPosition(holder.getAdapterPosition())).setFillName(item.getItem().getOptions().get(position).getName());
                                dicDataItems.get(getDicPosition(holder.getAdapterPosition())).getResult().getItems().get(getTempPosition(holder.getAdapterPosition())).setFillValue(item.getItem().getOptions().get(position).getName());
                            }
                        }).showAtLocation(tv_answer, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 1);
                        break;
                    case 7:
                        new DatePickerDialog(holder.getConvertView().getContext(), new DatePickerDialog.OnDateSetListener() {
                            @Override
                            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                                mCalendar.set(year, monthOfYear, dayOfMonth);
                                String data = DateUtil.date2Str(mCalendar, DateUtil.FORMAT_YMD);
                                if (!TextUtils.isEmpty(item.getItem().getMinValue()) && !DateUtil.CompareDate(DateUtil.DateTime2Str(item.getItem().getMinValue(), DateUtil.FORMAT_YMD), data, DateUtil.FORMAT_YMD)) {
                                    MessageTools.showLongToast("最小日期为：" + DateUtil.DateTime2Str(item.getItem().getMinValue(), DateUtil.FORMAT_YMD));
                                    return;
                                }
                                if (!TextUtils.isEmpty(item.getItem().getMaxValue()) && !DateUtil.CompareDate(data, DateUtil.DateTime2Str(item.getItem().getMaxValue(), DateUtil.FORMAT_YMD), DateUtil.FORMAT_YMD)) {
                                    MessageTools.showLongToast("最大日期为：" + DateUtil.DateTime2Str(item.getItem().getMaxValue(), DateUtil.FORMAT_YMD));
                                    return;
                                }
                                tv_answer.setText(data);
                                dicDataItems.get(getDicPosition(holder.getAdapterPosition())).getResult().getItems().get(getTempPosition(holder.getAdapterPosition())).setFillName(data);
                                dicDataItems.get(getDicPosition(holder.getAdapterPosition())).getResult().getItems().get(getTempPosition(holder.getAdapterPosition())).setFillValue(data);
                            }
                        }, mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH), mCalendar.get(Calendar.DAY_OF_MONTH)).show();
                        break;
                    default:
                        break;
                }
            }
        });
    }

    //竖坐标
    private int getDicPosition(int position) {
        return position / (items.size() + 1) - 1;
    }

    //横坐标
    private int getTempPosition(int position) {
        return position % (items.size() + 1) - 1;
    }
}
